﻿// 解决RadioGroup和CheckboxGroup的赋值问题

//在 Ext2.2的Ext.form.FormPanel中如果有RadioGroup或者CheckboxGroup，那么 FormPanel.form.setValues方法对这2个东东内部的radio和checkbox无法动态赋值，需要对 Ext.form.BasicForm的findField方法进行修正，代码如下：

/**
 * add two config items
 * @cfg {Boolean} unitedValue, if set to true, the value of checkboxgroup items will be set or got together
 *      and BasicForm.findField will return checkboxgroup instead of checkboxgroup's item. 
 *      At the same time, checkboxgroup items should have the same name.
 * @cfg {Boolean} trackResetOnLoad If set to true, form.reset() resets to the last loaded, when unitedValue is set to true. 
 *      When unitedValue is false, the items of CheckBoxGroup trackResetOnLoad on formpanel's config trackResetOnLoad.
 *      Default inherit from the basicform's config trackResetOnLoad.
 * @ there leaves a problem that when some of checkboxgroup items are not set with value and unitedValue is false, these items won't change values
 **/
Ext.override(Ext.form.CheckboxGroup, {
	/**
	 * This is a must in order not to save the value of checkboxgroup
	 */
	originalValue: undefined
		
    /**
     * Returns the string value
     * @return {Mixed} value RawValues of checkbox items in checkboxgroup with "," seperated
     */
    ,getValue: function() {
        var v = [];
        this.items.each(function(item) {
            if (item.getValue()) {
                v.push(item.getRawValue());
            }
        });
        return v.join(',');
    }
    
    /**
     * Sets the checked state of the checkbox items in checkboxgroup.
     * @param {String} v RawValues of checkbox items in checkboxgroup with "," seperated
     */
    ,setValue: function(v) {
      if (typeof(v) !== "string")
          return;
        var r = new RegExp('(' + v.replace(/,/g, "|") + ')');
        if (this.unitedValue){
	        this.items.each(function(item) {
	            item.setValue(v ? r.test(item.getRawValue()) : false);
	            if(this.trackResetOnLoad){
	                item.originalValue = item.getValue();
	            }
	        }, this);
        }
    }
    
    /**
     * check all checkbox in checkboxgroup
     */
    ,checkAll: function() {
	    this.items.each(function(item) {
	        item.setValue(true);
            if(this.trackResetOnLoad){
                item.originalValue = item.getValue();
            }
	    });
    }
    
    /**
     * uncheck all checkbox in checkboxgroup
     */
    ,uncheckAll: function() {
	    this.items.each(function(item) {
	        item.setValue(false);
            if(this.trackResetOnLoad){
                item.originalValue = item.getValue();
            }
	    });
    }
    
    /**
     * Returns true if any checkbox in this checkboxgroup have changed since their original load.
     * @return Boolean
     */
    ,isDirty : function(){
        var dirty = false;
        this.items.each(function(f){
           if(f.isDirty()){
               dirty = true;
               return false;
           }
        });
        return dirty;
    }

});

Ext.override(Ext.form.RadioGroup, {

	/**
	 * Returns name
	 */
	getName: function() {
	    return this.items.first().getName();
	}
  
    /**
     * Returns value
     * @return {Mixed} rawValue of radio item selected
     */
    ,getValue: function() {
        var v;
        this.items.each(function(item) {
            if (item.getValue()) {
                v = item.getRawValue();
                return false;
            }
        });
        return v;
    }
    
    /**
     * Sets the checked state of the checkbox items in checkboxgroup.
     * @param {String} v RawValues of checkbox items in checkboxgroup with "," seperated
     */
    ,setValue: function(v) {
      if (typeof(v) !== "string" && typeof(v) !== "number")
          return;
        this.items.each(function(item) {
            item.setValue(item.getRawValue() == v);
            if(this.trackResetOnLoad){
                item.originalValue = item.getValue();
            }
        }, this);
        
    }
});
